package com.zerro.litez.runtime.migration;



import java.util.*;

public class TableInfo {

    String tableSign;

    String tableName;
    String sql;

    String columnSignStr;
    List<String> columnSigns;

    List<ColumnInfo> columnInfoList = new ArrayList<>();

    List<ColumnInfo> primaryKeys = new ArrayList<>();

    ColumnInfo autoInc;

    Map<String, ColumnInfo> signColumnInfoHashMap = new HashMap<>();

    public TableInfo(String sign, String sql, String columnSigns) {

        this.tableSign = sign;
        this.sql = sql;
        this.columnSignStr = columnSigns;
        this.columnSigns = Arrays.asList(columnSigns.replaceAll(" ", "").split(","));
        onParse();


    }


    private void onParse() {
        String[] str = sql.split("[(]", 2);
        parseTableName(str[0]);

        parseColumns(str[1].substring(0, str[1].length() - 1));
    }

    private void parseTableName(String stmt) {
        String[] s = stmt.split(" ");
        tableName = s[s.length - 1];
    }


    private void parseColumns(String stmt) {
        String[] split = stmt.split(",primary key[(]");
        String[] strings = split[0].split(",");
        for (int i = 0; i < strings.length; i++) {
            String s = strings[i];
            ColumnInfo info = parseColumn(s);
            signColumnInfoHashMap.put(columnSigns.get(i), info);
        }
        if (split.length > 1) {
            parsePrimaryKeys(split[1]);
        }
    }

    private ColumnInfo parseColumn(String colDefines) {
        String[] columnDefine = colDefines.split(" ");
        ColumnInfo columnInfo = new ColumnInfo(colDefines);
        columnInfo.setName(columnDefine[0]);
        columnInfo.setType(columnDefine[1]);
        if (colDefines.contains("not null")) {
            columnInfo.setNotnull(true);
        }
        if (colDefines.contains("primary key")) {
            primaryKeys.add(columnInfo);
            if (colDefines.contains("autoincrement")) {
                autoInc = columnInfo;
            }
        }
        for (int i = 2; i < columnDefine.length; i++) {
            String define = columnDefine[i];
            if (define.equals("default")) {
                columnInfo.setDefaultValue(columnDefine[i + 1]);
                break;
            }
        }
        return columnInfo;
    }


    private void parsePrimaryKeys(String s) {
        String[] pks = s.substring(0, s.length() - 1).split(",");
        for (ColumnInfo columnInfo : columnInfoList) {
            for (String pk : pks) {
                if (columnInfo.getName().equals(pk)) {
                    primaryKeys.add(columnInfo);
                }
            }
        }
    }

    public String getTableSign() {
        return tableSign;
    }

    public String getTableName() {
        return tableName;
    }

    public String getSql() {
        return sql;
    }

    public List<String> getColumnSigns() {
        return columnSigns;
    }

    public String getColumnSignStr() {
        return columnSignStr;
    }

    public List<ColumnInfo> getColumnInfoList() {
        return columnInfoList;
    }

    public List<ColumnInfo> getPrimaryKeys() {
        return primaryKeys;
    }

    public ColumnInfo getAutoInc() {
        return autoInc;
    }

    public ColumnInfo getColumnInfo(String sign) {
        return signColumnInfoHashMap.get(sign);
    }

    @Override
    public String toString() {
        return "TableParser{" +
                "tableName='" + tableName + '\'' +
                ", sql='" + sql + '\'' +
                ", columnInfoList=" + columnInfoList +
                ", \nprimaryKeys=" + primaryKeys +
                ", \nautoInc=" + autoInc +
                '}';
    }
}
